package com.leo.chapter07;

import java.math.BigInteger;
import java.util.concurrent.BlockingQueue;

/**
 *
 * @author xuexiaolei
 * @version 2017年10月16日
 */
public class PrimeProducer extends Thread {
    private final BlockingQueue<BigInteger> queue;

    public PrimeProducer(BlockingQueue<BigInteger> queue) {
        this.queue = queue;
    }

    public void run(){
        try {
            BigInteger p = BigInteger.ONE;
            while (!Thread.currentThread().isInterrupted()){
                queue.put(p.nextProbablePrime());
            }
        } catch (InterruptedException e) {
            this.cancell();
        }
    }

    private void cancell() {
        Thread.currentThread().interrupt();
    }
}
